package com.qing.common.utils.sm;

import cn.hutool.crypto.asymmetric.SM2;
import cn.hutool.crypto.symmetric.SM4;

/**
 * @author guoqf
 * @date 2022/10/12 2:04
 */
public class Sm2AndSm4 {
    public static void main(String[] args) {
        String source = "1234567890qwertyuiop1234567890qwertyuiopasdfghjklzxcvbnm1234567890qwertyuiopasdfghjklzxcvbnm1234567890qwertyuiopasdfghjklzxcvbnm1234567890qwertyuiopasdfghjklzxcvbnm1234567890qwertyuiopasdfghjklzxcvbnm1234567890qwertyuiopasdfghjklzxcvbnm1234567890qwertyuiopasdfghjklzxcvbnm1234567890qwertyuiopasdfghjklzxcvbnm1234567890qwertyuiopasdfghjklzxcvbnm1234567890qwertyuiopasdfghjklzxcvbnm1234567890qwertyuiopasdfghjklzxcvbnm11234567890qwertyuiop1234567890qwertyuiopasdfghjklzxcvbnm1234567890qwertyuiopasdfghjklzxcvbnm1234567890qwertyuiopasdfghjklzxcvbnm1234567890qwertyuiopasdfghjklzxcvbnm1234567890qwertyuiopasdfghjklzxcvbnm1234567890qwertyuiopasdfghjklzxcvbnm1234567890qwertyuiopasdfghjklzxcvbnm1234567890qwertyuiopasdfghjklzxcvbnm1234567890qwertyuiopasdfghjklzxcvbnm1234567890qwertyuiopasdfghjklzxcvbnm1234567890qwertyuiopasdfghjklzxcvbnm1234567890qwertyu1234567890qwertyuiop1234567890qwertyuiopasdfghjklzxcvbnm1234567890qwertyuiopasdfghjklzxcvbnm1234567890qwertyuiopasdfghjklzxcvbnm1234567890qwertyuiopasdfghjklzxcvbnm1234567890qwertyuiopasdfghjklzxcvbnm1234567890qwertyuiopasdfghjklzxcvbnm1234567890qwertyuiopasdfghjklzxcvbnm1234567890qwertyuiopasdfghjklzxcvbnm1234567890qwertyuiopasdfghjklzxcvbnm1234567890qwertyuiopasdfghjklzxcvbnm1234567890qwertyuiopasdfghjklzxcvbnm1234567890qwertyuiopasdfghjklzxcvbnm1234567890qwertyuiopasdfghjklzxcvbnm1234567890qwertyuiopasdfghjklzxcvbnmasdfghjklzxcvbnm1234567890qwertyuiop1234567890qwertyuiopasdfghjklzxcvbnm1234567890qwertyuiopasdfghjklzxcvbnm1234567890qwertyuiopasdfghjklzxcvbnm1234567890qwertyuiopasdfghjklzxcvbnm1234567890qwertyuiopasdfghjklzxcvbnm1234567890qwertyuiopasdfghjklzxcvbnm1234567890qwertyuiopasdfghjklzxcvbnm1234567890qwertyuiopasdfghjklzxcvbnm1234567890qwertyuiopasdfghjklzxcvbnm1234567890qwertyuiopasdfghjklzxcvbnm1234567890qwertyuiopasdfghjklzxcvbnm1234567890qwertyuiopasdfghjklzxcvbnm1234567890qwertyuiopasdfghjklzxcvbnm1234567890qwertyuiopasdfghjklzxcvbnmasdfghjklzxcvbnmiopasdfghjklzxcvbnm1234567890qwertyuiopasdfghjklzxcvbnm1234567890qwertyuiopasdfghjklzxcvbnmasdfghjklzxcvbnm234567890qwertyuiopasdfghjklzxcvbnm1234567890qwertyuiopasdfghjklzxcvbnm1234567890qwertyuiopasdfghjklzxcvbnmasdfghjklzxcvbnm";
        byte[] srcBytes = source.getBytes();

        int loop = 1;
        sm4Loop(loop, srcBytes);
        sm2Loop(loop, srcBytes);
        loop = 10;
        sm4Loop(loop, srcBytes);
        sm2Loop(loop, srcBytes);
        loop = 100;
        sm4Loop(loop, srcBytes);
        sm2Loop(loop, srcBytes);
        loop = 1000;
        sm4Loop(loop, srcBytes);
        sm2Loop(loop, srcBytes);
        loop = 10000;
        sm4Loop(loop, srcBytes);
        sm2Loop(loop, srcBytes);





    }

    private static void sm4Loop(int loop, byte[] source) {
        SM4 sm4 = new SM4();
        sm4.encrypt(source);
        System.out.println("SM4 loop times : " + loop);
        long start = System.currentTimeMillis();
        for (int i = 0; i < loop; i++) {
            sm4.encrypt(source);
        }

        System.out.println("SM4 used time: " + (System.currentTimeMillis() - start) + " ms");

    }

    private static void sm2Loop(int loop, byte[] source) {
        SM2 sm2 = new SM2();
        sm2.encrypt(source);
        System.out.println("SM2 loop times : " + loop);
        long start = System.currentTimeMillis();
        for (int i = 0; i < loop; i++) {
            sm2.encrypt(source);
        }

        System.out.println("SM2 used time: " + (System.currentTimeMillis() - start) + " ms");

    }

    private static void sm2AndSm4CombineEncrypt(byte[] source, byte[] pubKey) {
        // 创建SM4对称加密工具
        SM4 sm4 = new SM4();
        // 获取SM4密钥
        byte[] symmKey = sm4.getSecretKey().getEncoded();
        // 创建SM2非对称加密工具
        SM2 sm2 = new SM2(null, pubKey);
        // 使用SM2算法对SM4的密钥进行加密：①密钥密文
        byte[] keyCipher = sm2.encrypt(symmKey);
        // 使用SM4算法对原始消息进行加密：②数据密文
        byte[] symmCipher = sm4.encrypt(source);

    }

    private static void sm2AndSm4CombineDecrypt(byte[] cipher, byte[] priKey, byte[] symmKey) {
        // 创建SM2非对称解密工具
        SM2 sm2 = new SM2(priKey, null);
        // 使用SM2算法对SM4的密钥进行解密：①对称密钥
        byte[] keyCipher = sm2.encrypt(symmKey);
        // 使用对称密钥创建SM4对称解密工具
        SM4 sm4 = new SM4(keyCipher);
        // 使用SM4算法对数据密文进行解密：②原始消息
        byte[] source = sm4.decrypt(cipher);

    }
}
